<html>
<head><meta charset="utf-8"><title>Getting trait of an impl · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Getting.20trait.20of.20an.20impl.html">Getting trait of an impl</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="240920585"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Getting%20trait%20of%20an%20impl/near/240920585" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jade <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Getting.20trait.20of.20an.20impl.html#240920585">(Jun 01 2021 at 10:13)</a>:</h4>
<p>I'm currently working on a feature to be able to goto definition a trait impl to get to the trait on the other side (kinda like <a href="https://github.com/rust-analyzer/rust-analyzer/pull/8988">https://github.com/rust-analyzer/rust-analyzer/pull/8988</a> but the other way around) and I'm having trouble actually finding the trait. I'm currently trying to do it through the route of getting the AssocItem and then the container, to get the impl.</p>
<p>From that impl, I can get a TraitRef, but as per the comment:</p>
<div class="codehilite"><pre><span></span><code>    // FIXME: the return type is wrong. This should be a hir version of
    // `TraitRef` (ie, resolved `TypeRef`).
</code></pre></div>
<p>this is not a really useful object to have, as is also supported by poking through the rustdocs. So I'm stuck here.</p>
<p>To be specific, I want to get the trait items in such a way that I can find the function on the trait itself matching the name of the function on the impl, then get its position.</p>



<a name="240925836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Getting%20trait%20of%20an%20impl/near/240925836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Getting.20trait.20of.20an.20impl.html#240925836">(Jun 01 2021 at 11:10)</a>:</h4>
<p>you can use <code>db.impl_trait</code> to get the implement Chalk <code>TraitRef</code>; then you can use <code>hir_ty::from_chalk_trait_id(trait_ref.trait_id)</code> to get the trait ID and turn that into a <code>hir::Trait</code>. I think you could make that function just return <code>Option&lt;Trait&gt;</code> for now; it seems all uses of it just call <code>is_some</code> or <code>is_none</code> anyway?</p>



<a name="240925905"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Getting%20trait%20of%20an%20impl/near/240925905" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Getting.20trait.20of.20an.20impl.html#240925905">(Jun 01 2021 at 11:11)</a>:</h4>
<p>ideally we'd have something like <code>hir::Type</code> that wraps a <code>chalk_ir::TraitRef</code> there (that's what the FIXME means), but for now just returning the <code>Trait</code>would be fine</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>